const { $util } = getApp();
const app = getApp();
const myaudio = wx.createInnerAudioContext();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loaded: false,
    status: 0, // 0 还未开始 1 进行中 2 结束
    isOld: true, // 添加活动报名之前的旧数据
  },
  register() { // 参加报名
    let that = this;
    $util.request({
      url: 'entry/wxapp/edu.activity.signup',
      data: {
        id: this.data.id,
      },
      success: (res) => {
        wx.showToast({
          title: '报名成功',
          mask: true,
          duration: 1e3,
          success: function () {
            setTimeout(function () {
              that.loadDetail();
            }, 1000);
          },
        });
        
      },
    });
  },
  onShow: function() {
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
    this.loadDetail();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData(options);
  },
  loadDetail() {
    $util.request({
      url: 'entry/wxapp/edu.activity.details',
      data: {
        id: this.data.id,
      },
      success: (res) => {
        // res.list && res.list.title && wx.setNavigationBarTitle({
        //   title: res.list.title,
        // });
        wx.setNavigationBarTitle({
          title: '主题活动',
        })
        if (res.list && res.list.video) {
          const arrs = res.list.video.split('.');
          const arritem = arrs[arrs.length - 1].split('?');
          res.list.isvideo = arritem[0];
          myaudio.src = res.list.video;
        }
        res.list.loaded = true;
        this.setData(res.list);

        if (res.list.actstarttime == 0 || res.list.actendtime == 0) { // 之前的数据没有活动的开始结束时间  数据为0
          this.setData({
            isOld: true,
          });
          return;
        } else {
          this.setData({
            isOld: false,
          });
        }

        let nowTime = new Date().getTime();
        let stTimeStr = res.list.actstarttime.replace(/-/g, '/');
        let endTimeStr = res.list.actendtime.replace(/-/g, '/');
        let stTime = new Date(stTimeStr).getTime();
        let endTime = new Date(endTimeStr).getTime();
  
        if (stTime > nowTime) { // 未开始
          this.setData({
            status: 0,
          });
        }
        if (nowTime >= stTime && nowTime < endTime) { // 活动进行中
          this.setData({
            status: 1,
          });
        }
        if (nowTime >= endTime) { // 活动结束
          this.setData({
            status: 2,
          });
        }
    
      },
    });
  },
  goList() {
    let url = '/ws_party_const/pages/education/activity/theme/list';
    app.util.navigateTo(url, {
      id: this.data.id,
    });
  },
  audioPlay(e) {
    myaudio.play();
    this.setData({
      isplay: true,
    });
  },
  audioPaused(e) {
    myaudio.pause();
    this.setData({
      isplay: false,
    });
  },
  onUnload() {
    myaudio.destroy();
  },
  onShareAppMessage() {
    let _this = this;
    let path = '/ws_party_const/pages/education/activity/theme/detail?id' + _this.data.id;
    let res = {
      path,
    };
    return res;
  },
});
